<?php

namespace app\libs\DianDianPaySDK\builder;


use app\helpers\UtilHelper;
use app\model\LineItems;
use app\model\Orders;
use app\trait\PaymentTrait;
use think\helper\Arr;
use think\Request;

class DianDianPaymentBuilder
{
    use PaymentTrait;
    private string $merchant_id;
    private array $card = [];

    private string $merchant_order_id;

    public function getCard(): array
    {
        return $this->card;
    }

    public function setCard(array $card): DianDianPaymentBuilder
    {
        $this->card = $card;
        return $this;
    }

    public function getMerchantId(): string
    {
        return $this->merchant_id;
    }

    public function setMerchantId(string $merchant_id): DianDianPaymentBuilder
    {
        $this->merchant_id = $merchant_id;
        return $this;
    }
    public function __construct(protected Orders $order,protected Request $request)
    {
    }

    public function toArray()
    {
        list($return_url,$cancel_url) = Orders::getContextApplicationUrl($this->order,$this->request);
        return [
            "merchant_id"=>$this->getMerchantId(),
            'env'=>$this->getEnv(),
            'order'=>$this->getOrder(),
            'redirect_url'=>$return_url,
        ];
    }

    private function getEnv():array
    {
        $user_agent = $this->request->header("user-agent");
        return [
            "terminal_type"=>UtilHelper::isMobile($user_agent) ? "WAP" :"WEB",
            "client_ip"=>UtilHelper::getClientIP($this->request),
            "browser_info"=>[
                "user_agent"=>$user_agent
            ],
        ];
    }

    private function getOrder():array
    {
        return [
            "merchant_order_id"=>$this->getMerchantOrderId(),
            "shipping"=>$this->getShipping(),
            "payment_amount"=>[
                "currency"=>$this->order->shop_currency,
                "value"=>intval(bcmul($this->order->total_shop, "100"))
            ],
            "payment_method"=>$this->getPaymentMethod(),
            "metadata"=>$this->setMetadata(),
        ];
    }

    private function getShipping():array
    {
        return [
            "shipping_name"=>[
                "first_name"=>$this->order->shippingAddress->first_name,
                "last_name"=>$this->order->shippingAddress->last_name,
                "full_name"=>$this->order->shippingAddress->name,
            ],
            "shipping_address"=>[
                "country"=> $this->order->shippingAddress->country_code,
                "state"=> $this->order->shippingAddress->province,
                "city"=> $this->order->shippingAddress->city,
                "address1"=>$this->order->shippingAddress->address1,
                "zip_code"=> $this->order->shippingAddress->zip
            ],
            "email"=>$this->order->email,
            "phone"=>$this->order->shippingAddress->phone,
            //"carrier"=>"USPS"
        ];
    }

    private function getPaymentMethod():array
    {
        $card = $this->getCard();
        $card_holder = Arr::get($card,'cardholder');
        $holder_name_arr = explode(" ",$card_holder);
        return [
            "payment_type"=>"CARD",
            "payment_data"=>[
                "country"=>$this->order->billingAddress->country_code,
                "card_number"=>Arr::get($card,'cardNumber'),
                "expiry_year"=>Arr::get($card,'expiredYear'),
                "expiry_month"=>Arr::get($card,'expiredMonth'),
                "cvv"=>Arr::get($card,'cvv'),
                "card_holder_name"=>[
                    "first_name"=>Arr::first($holder_name_arr),
                    "last_name"=>Arr::last($holder_name_arr),
                    "full_name"=>$card_holder,
                ],
                "billing_address"=>[
                    "country"=> $this->order->billingAddress->country_code,
                    "state"=> $this->order->billingAddress->province,
                    "city"=> $this->order->billingAddress->city,
                    "address1"=>$this->order->billingAddress->address1,
                    "zip_code"=> $this->order->billingAddress->zip
                ],
                "requires_3ds"=>env('ENABLE_3DS',false)
            ],
        ];
    }

    private function setMetadata():array
    {
        return [
            "order_id"=>strval($this->order->id),
            //"domain"=>$this->order->notify_url,
        ];
    }

    private function getGoods():array
    {
        $list = [];
        $this->order->items()->select()->each(function (LineItems $product) use (&$list){
            $list[] = [
                "goods_id"=>uniqid(time()),
                "goods_name"=>$product->title,
                "goods_category"=>"test",
                "goods_quantity"=>$product->quantity,
                "goods_price"=>intval(bcmul($product->price,"100")),
                "goods_url"=>"",
                "delivery_method_type"=>"PHYSICAL"
            ];
        });
        return $list;
    }

    public function getMerchantOrderId(): string
    {
        return $this->merchant_order_id;
    }

    public function setMerchantOrderId(string $merchant_order_id): DianDianPaymentBuilder
    {
        $this->merchant_order_id = $merchant_order_id;
        return $this;
    }


}